package com.shyt.api;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.shyt.dao.RoleMenuSqRequstVo;
import com.shyt.dao.SysPermissionVo;
import com.shyt.dao.SysRoleVo;
import com.shyt.filter.annotation.RecordLogsAspectAt;
import com.shyt.impl.SysPermissionServiceImpl;
import com.shyt.util.ApiResponse;
import com.shyt.util.CommonUtil;
import com.shyt.util.MenuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Slf4j
@RequestMapping("api/")
@CrossOrigin
public class SysMenuApi {
    @Autowired
    SysPermissionServiceImpl sysPermissionService;
    @Autowired
    RedisTemplate redisTemplate;

    @RecordLogsAspectAt
    @PostMapping("addMenu")
    public ApiResponse addMenu(@RequestBody SysPermissionVo sysPermissionVo) throws Exception {
        Date now = new Date();
        Integer parentId = sysPermissionVo.getType();
        if (parentId == 0){
            sysPermissionVo.setParentName("顶级菜单");
            sysPermissionVo.setParentId(0l);
        }
        sysPermissionVo.setId(getLong(new Random(), 100000l));
        sysPermissionVo.setCreateTime(CommonUtil.sdf20.format(now));
        sysPermissionVo.setUpdateTime(CommonUtil.sdf20.format(now));
        sysPermissionVo.setPath('/'+sysPermissionVo.getCode());
        sysPermissionVo.setName(sysPermissionVo.getCode());
        sysPermissionVo.setCode("sys:"+sysPermissionVo.getCode());
        sysPermissionVo.setUrl("system"+ sysPermissionVo.getPath().trim() +
                (0 == sysPermissionVo.getType() ? "" :  sysPermissionVo.getPath().trim()));
        sysPermissionService.addMenu(sysPermissionVo);
        //return new ApiResponse("200", "调用成功", "新增成功");
        return ApiResponse.ok("新增成功");
    }

    @RecordLogsAspectAt
    @PostMapping("editMenu")
    public ApiResponse editMenu(@RequestBody SysPermissionVo sysPermissionVo) throws Exception {
        //sysPermissionVo.setId(new Random().nextLong());
        //sysPermissionVo.setCreateTime(new Date());
        Date now = new Date();
        sysPermissionVo.setUpdateTime(CommonUtil.sdf20.format(now));
        sysPermissionService.editMenu(sysPermissionVo);
        //return new ApiResponse("200", "调用成功", "编辑成功");
        return ApiResponse.ok("编辑成功");
    }

    @RecordLogsAspectAt
    @PostMapping("deleteMenuRoles")
    public ApiResponse deleteMenuRoles(@RequestBody SysPermissionVo sysPermissionVo) throws Exception {
        sysPermissionService.deleteMenuRoles(sysPermissionVo);
        //return new ApiResponse("200", "调用成功", "编辑成功");
        return ApiResponse.ok("删除成功");
    }

    @RecordLogsAspectAt
    @PostMapping("selectMenuXlk")
    public ApiResponse selectMenuXlk() throws Exception {
        List<SysPermissionVo> sysPermissionVos = sysPermissionService.selectMenuXlk();

        List<Map> rtnList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysPermissionVos)) {
            sysPermissionVos.stream().forEach(item -> {
                Map<String, Object> rtnMap = new HashMap<>();
                rtnMap.put("value", item.getId());
                rtnMap.put("label", item.getLabel());
                rtnMap.put("level", item.getType());
                rtnList.add(rtnMap);
            });
        }

        //return new ApiResponse("200", "调用成功", rtnList);
        return ApiResponse.ok(rtnList);
    }

    @RecordLogsAspectAt
    @PostMapping("selectMenuTypeXlk")
    public ApiResponse selectMenuTypeXlk() throws Exception {
        List<SysPermissionVo> sysPermissionVos = sysPermissionService.selectMenuTypeXlk();

        List<Map> rtnList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysPermissionVos)) {
            sysPermissionVos.stream().forEach(item -> {
                Map<String, Object> rtnMap = new HashMap<>();
                rtnMap.put("value", item.getType());
                rtnMap.put("label", item.getType() == 0 ? "一级菜单" :
                        (item.getType() == 1 ? "二级菜单" : item.getType()));
                rtnMap.put("level", item.getType());
                rtnList.add(rtnMap);
            });

        }

        //return new ApiResponse("200", "调用成功", rtnList);
        return ApiResponse.ok(rtnList);
    }

    @RecordLogsAspectAt
    @PostMapping("selectAllMenus")
    public ApiResponse selectAllMenus(@RequestBody SysPermissionVo sysPermissionVo) throws Exception {

        List<SysPermissionVo> sysPermissionVos = sysPermissionService.selectAllMenus(sysPermissionVo);
        List<SysPermissionVo> menus = MenuUtil.getMenu(sysPermissionVos);

        //return new ApiResponse("200", "调用成功", menus);
        return ApiResponse.ok(menus);
    }


    public long getLong(Random random, long n) {
        long bits, val;
        do {
            bits = (random.nextLong() << 1) >>> 1;
            val = bits % n;
        } while (bits - val + (n - 1) < 0l);
        return val;
    }


    /**
     * 查询菜单已经授权的角色
     *
     * @param sysPermissionVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("selectRolesByMeunId")
    public ApiResponse selectRolesByMeunId(@RequestBody SysPermissionVo sysPermissionVo) throws Exception {

        List<SysRoleVo> sysRoleVos = sysPermissionService.selectRolesByMeunId(sysPermissionVo);

        List<Map> maps = new ArrayList<>();
        if (CollectionUtils.isEmpty(sysRoleVos)) {
            //return new ApiResponse("200", "调用成功", maps);
            return ApiResponse.ok(maps);
        }

        for (int i = 0; i < sysRoleVos.size(); i++) {
            SysRoleVo sysRoleVo = sysRoleVos.get(i);
            HashMap<String, Object> rtnMap = new HashMap<>();
            rtnMap.put("label", sysRoleVo.getRoleName());
            rtnMap.put("key", sysRoleVo.getId());
            //rtnMap.put("pinyin", sysRoleVo.getId());
            maps.add(rtnMap);
        }

        //return new ApiResponse("200", "调用成功", maps);
        return ApiResponse.ok(maps);
    }


    /**
     * 查询菜单还未授权的角色
     *
     * @param sysPermissionVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("selectWSQRolesByMeunId")
    public ApiResponse selectWSQRolesByMeunId(@RequestBody SysPermissionVo sysPermissionVo) throws Exception {

        List<SysRoleVo> sysRoleVos = sysPermissionService.selectWSQRolesByMeunId(sysPermissionVo);

        List<Map> maps = new ArrayList<>();
        if (CollectionUtils.isEmpty(sysRoleVos)) {
            //return new ApiResponse("200", "调用成功", maps);
            return ApiResponse.ok(maps);
        }

        for (int i = 0; i < sysRoleVos.size(); i++) {
            SysRoleVo sysRoleVo = sysRoleVos.get(i);
            HashMap<String, Object> rtnMap = new HashMap<>();
            rtnMap.put("label", sysRoleVo.getRoleName());
            rtnMap.put("key", sysRoleVo.getId());
            //rtnMap.put("pinyin", "wsq"+sysRoleVo.getRoleCode());
            maps.add(rtnMap);
        }

        //return new ApiResponse("200", "调用成功", maps);
        return ApiResponse.ok(maps);
    }

    /**
     * 菜单授权处理
     *
     * @param roleMenuSqRequstVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("roleMenuEdit")
    public ApiResponse roleMenuEdit(@RequestBody RoleMenuSqRequstVo roleMenuSqRequstVo) throws Exception {
        sysPermissionService.roleMenuEdit(roleMenuSqRequstVo);
        //return new ApiResponse("200", "调用成功", null);
        return ApiResponse.ok("");
    }

}
